//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.util;

import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils;
import org.yaukie.base.html.EscapeUtil;

public class IpUtil {
    private static final Logger log = LoggerFactory.getLogger(IpUtil.class);
    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
    public static final String UNKNOWN = "XX XX";

    public IpUtil() {
    }

    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        } else {
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : EscapeUtil.clean(ip);
        }
    }

    public static boolean internalIp(String ip) {
        byte[] addr = textToNumericFormatV4(ip);
        return internalIp(addr) || "127.0.0.1".equals(ip);
    }

    private static boolean internalIp(byte[] addr) {
        if (!StringTools.isNull(addr) && addr.length >= 2) {
            byte b0 = addr[0];
            byte b1 = addr[1];
            switch (b0) {
                case -84:
                    if (b1 >= 16 && b1 <= 31) {
                        return true;
                    }
                case -64:
                    switch (b1) {
                        case -88:
                            return true;
                    }
                default:
                    return false;
                case 10:
                    return true;
            }
        } else {
            return true;
        }
    }

    public static byte[] textToNumericFormatV4(String text) {
        if (text.length() == 0) {
            return null;
        } else {
            byte[] bytes = new byte[4];
            String[] elements = text.split("\\.", -1);

            try {
                long l;
                int i;
                switch (elements.length) {
                    case 1:
                        l = Long.parseLong(elements[0]);
                        if (l < 0L || l > 4294967295L) {
                            return null;
                        }

                        bytes[0] = (byte)((int)(l >> 24 & 255L));
                        bytes[1] = (byte)((int)((l & 16777215L) >> 16 & 255L));
                        bytes[2] = (byte)((int)((l & 65535L) >> 8 & 255L));
                        bytes[3] = (byte)((int)(l & 255L));
                        break;
                    case 2:
                        l = (long)Integer.parseInt(elements[0]);
                        if (l >= 0L && l <= 255L) {
                            bytes[0] = (byte)((int)(l & 255L));
                            l = (long)Integer.parseInt(elements[1]);
                            if (l < 0L || l > 16777215L) {
                                return null;
                            }

                            bytes[1] = (byte)((int)(l >> 16 & 255L));
                            bytes[2] = (byte)((int)((l & 65535L) >> 8 & 255L));
                            bytes[3] = (byte)((int)(l & 255L));
                            break;
                        }

                        return null;
                    case 3:
                        for(i = 0; i < 2; ++i) {
                            l = (long)Integer.parseInt(elements[i]);
                            if (l < 0L || l > 255L) {
                                return null;
                            }

                            bytes[i] = (byte)((int)(l & 255L));
                        }

                        l = (long)Integer.parseInt(elements[2]);
                        if (l < 0L || l > 65535L) {
                            return null;
                        }

                        bytes[2] = (byte)((int)(l >> 8 & 255L));
                        bytes[3] = (byte)((int)(l & 255L));
                        break;
                    case 4:
                        for(i = 0; i < 4; ++i) {
                            l = (long)Integer.parseInt(elements[i]);
                            if (l < 0L || l > 255L) {
                                return null;
                            }

                            bytes[i] = (byte)((int)(l & 255L));
                        }

                        return bytes;
                    default:
                        return null;
                }

                return bytes;
            } catch (NumberFormatException var6) {
                return null;
            }
        }
    }

    public static String getHostIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException var1) {
            return "127.0.0.1";
        }
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException var1) {
            return "未知";
        }
    }

    public static String getRealAddressByIP(String ip) {
        String address = "XX XX";
        if (internalIp(ip)) {
            return "内网IP";
        } else {
            try {
                String rspStr = HttpUtils.sendGet("http://whois.pconline.com.cn/ipJson.jsp", "ip=" + ip + "&json=true", "GBK");
                if (StringTools.isEmpty(rspStr)) {
                    log.error("获取地理位置异常 {}", ip);
                    return "XX XX";
                } else {
                    JSONObject obj = JSONObject.parseObject(rspStr);
                    String region = obj.getString("pro");
                    String city = obj.getString("city");
                    return String.format("%s %s", region, city);
                }
            } catch (Exception var6) {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
        }
    }

    public static void main(String[] args) {
        String base64 = "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AKADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygp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ReGnw7s7BVqKBIxwMVkf2vcf3IvyP8AjS/2zcf3IvyP+NH1qHZh7RG6BTxWB/bVz/ci/I/40v8Ablz/AM84fyP+NH1qHZh7RG60SuMEUkdqiHIUVif27df884fyP+NL/b91/wA84f8Avk/40fWodmHtEdIowKeK5n/hILv/AJ5w/wDfJ/xpf+Eiu/8AnnB/3yf8aPrUOzD2iOoFNeFZOormv+EkvP8AnlB/3yf8aX/hJbz/AJ5Qf98n/Gj61Dsw9ojpYrdEPAqyBXJf8JPe/wDPK3/75P8AjS/8JRe/88rf/vlv8aPrUOzD2iOvFQaj/wAgu7/64v8A+gmuY/4Sq+/55W//AHy3+NMm8TXs9vJC0VuFkUoSFOcEY9ameJg4tWYOorH/2Q";
        byte[] bytes = null;
        File file = new File("D:\\test.jpg");
        bytes = Base64Utils.decodeFromString(base64);

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file, true);
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
        } catch (FileNotFoundException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

    }
}
